//
//  WCG_M_HistoryController.m
//  Project_B
//
//  Created by 甘霞 on 15/11/26.
//  Copyright © 2015年 Atom_Wang. All rights reserved.
//

#import "WCG_M_HistoryController.h"
#import "GXiaNetHandle.h"
#import "GXDailyNormalView.h"
#import "GXDailyModel.h"
#import "GXSuggestModel.h"
#import "WCG_M_HistoryDailyController.h"
#import "UIColor+RandomColor.h"
#import "GXSpecialCommentTableViewCell.h"
#import "GXPicturetableViewCell.h"
#import "GXDailyBigAlbumTableViewCell.h"
#import "NSString+GXSizeWithText.h"
#import "WCG_M_DailyClickController.h"

@interface WCG_M_HistoryController () <UITableViewDataSource, UITableViewDelegate, GXDailyViewDelegate>

@property (nonatomic, retain) GXDailyNormalView *dailyView;


@end

@implementation WCG_M_HistoryController

#pragma mark - 视图将要出现
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    //  隐藏导航栏
    [self.navigationController.navigationBar setHidden:YES];
    //  隐藏 tabBar
    NSArray *temp = self.tabBarController.view.subviews;
    UIView *view = temp[2];
    [UIView animateWithDuration:1.0f
                     animations:^{
                         view.frame = CGRectMake(0, kScreenHeight, kScreenWidth, 49);
                     }];
}

#pragma mark - 视图将要消失
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    //  隐藏 tabBar
    NSArray *temp = self.tabBarController.view.subviews;
    UIView *view = temp[2];
    [UIView animateWithDuration:1.0f
                     animations:^{
                         view.frame = CGRectMake(0, kScreenHeight - 49, kScreenWidth, 49);
                     }];
}

#pragma mark - 视图加载完成
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.view setBackgroundColor:[UIColor whiteColor]];
    
    //  创建 tableView
    [self createTableView];
    //  创建下午茶视图
    [self createDailyView];
    //  小菊花
    [self loadNightMBProgress];
    //  请求今日下午茶数据
    [self getTodayNoonData];
}

#pragma mark - 创建底部按钮
- (void)createButtomBtn {
    //  创建底部按钮
    [self createButtomBackButtonIsHiddenShareBtn:YES];
    //  回退
    [self.backBtn addTarget:self
                     action:@selector(btnAction:)
           forControlEvents:UIControlEventTouchUpInside];
}

//  按钮响应方法
- (void)btnAction:(UIButton *)btn {
    if (btn == self.backBtn) {
        [self.navigationController popViewControllerAnimated:YES];
    }
}

#pragma mark - dailyView 响应方法
//  下拉
- (void)gestureRecongnizerAction {
    
    [self loadNightMBProgress];
    [UIView animateWithDuration:3.0f
                     animations:^{
                         
                         //  dailyView 下滑
                         self.dailyView.frame = CGRectMake(0, kScreenHeight, kScreenWidth, kScreenHeight);
                         //  创建底部按钮
                         [self createButtomBtn];
                     } completion:^(BOOL finished) {
                     }];
    
    //  多线程
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3.0f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        //  动画
        [UIView animateWithDuration:2.0f
                         animations:^{
                             //  tableView 出现
                             self.tableView.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight);
                             [self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]
                                                   atScrollPosition:UITableViewScrollPositionTop
                                                           animated:YES];
                         } completion:^(BOOL finished) {
                             //  移除小菊花
                             [self removeHud];
                         }];
        
    });
}

//  历史记录按钮
- (void)dailyHistoryClickProtocol {
    WCG_M_HistoryDailyController *historyVC = [[WCG_M_HistoryDailyController alloc] init];
    [self.navigationController pushViewController:historyVC animated:YES];
}

//  返回按钮
- (void)dailuBackClickProtocol {
    [self.navigationController popToRootViewControllerAnimated:YES];
}

#pragma mark - 请求数据
- (void)getTodayNoonData {
    //  初始化数组
    self.dailyContentArr = [NSMutableArray array];
    //  多线程请求数据
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        //  移除小菊花
        [self removeHud];
        //请求
        [GXiaNetHandle getUrl:[NSString stringWithFormat:kDailyHostoryWebAppend, self.release_date] cookie:nil success:^(id result) {
            self.dailyModel = [GXDailyModel modelWithDic:result[@"daily"]];
            NSLog(@"~~~~~~~~~~~~~~~~~~~~~~~%@", self.dailyModel.title);
            for (NSDictionary *dic in self.dailyModel.contents) {
                GXSuggestModel *suggestModel = [GXSuggestModel modelWithDic:dic[@"item"]];
                //14 致谢 11 想问你
                if (suggestModel.type.integerValue == 14 || suggestModel.type.integerValue == 11) {
                    continue;
                }
                [self.dailyContentArr addObject:suggestModel];
                if (self.dailyContentArr.count) {
                    
                    [self.tableView reloadData];
                }
            }
        } failure:^(NSError *error) {
            NSLog(@"%@", error);
        }];
        
    });
}

#pragma mark - 创建 dailyView
- (void)createDailyView {
    //  创建 dailyView
    self.dailyView = [[GXDailyNormalView alloc] initWithFrame:self.view.frame
                                                   dailyModel:self.dailyModel];
    //  实时显示
    [self.dailyView setNeedsDisplay];
    //  代理
    self.dailyView.delegate = self;
    //  添加到父视图
    [self.view addSubview:self.dailyView];
}

# pragma mark - 创建 tabelView
- (void)createTableView {
    //  创建 tableView 在不可视范围内
    [self createTableViewWithFrame:CGRectMake(0, kScreenHeight, kScreenWidth, kScreenHeight)];
    //  代理
    self.tableView.delegate = self;
    //  数据源
    self.tableView.dataSource = self;
    //  注册 cell
    [self.tableView registerClass:[GXSpecialCommentTableViewCell class] forCellReuseIdentifier:@"dailyCell"];
    [self.tableView registerClass:[GXPicturetableViewCell class] forCellReuseIdentifier:@"pictureCell"];
    [self.tableView registerClass:[GXDailyBigAlbumTableViewCell class] forCellReuseIdentifier:@"bigAlbum"];
    [self.tableView registerClass:[UITableViewCell class]
           forCellReuseIdentifier:@"cell"];
}

//#pragma mark - tableView 代理/数据源
- (NSInteger)tableView:(UITableView *)tableView
 numberOfRowsInSection:(NSInteger)section {
    
    return self.dailyContentArr.count != 0 ? self.dailyContentArr.count : 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    //普通cell
    GXSpecialCommentTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"dailyCell"];
    //九宫格 cell
    GXPicturetableViewCell *picCell = [tableView dequeueReusableCellWithIdentifier:@"pictureCell"];
    //大型相册 cell
    GXDailyBigAlbumTableViewCell *bigAlbumCell = [tableView dequeueReusableCellWithIdentifier:@"bigAlbum"];
    //取得模型
    GXSuggestModel *suggestModel = self.dailyContentArr[indexPath.row];
    
    //文章/音乐/视频
    if (suggestModel.type.integerValue == 0
        || suggestModel.type.integerValue == 10
        || suggestModel.type.integerValue == 2)  {
        
        cell.suggestModel = suggestModel;
        return cell;
    }
    //九宫格
    else if (suggestModel.type.integerValue == 1) {
        picCell.suggestModel = suggestModel;
        return picCell;
    }
    //大型画册
    else if (suggestModel.type.integerValue == 9) {
        bigAlbumCell.suggestModel = suggestModel;
        return bigAlbumCell;
    }
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView
heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UIImage *qouteImageSize = [UIImage imageNamed:@"icon-quote-black"];
    //取得模型
    GXSuggestModel *suggestModel = self.dailyContentArr[indexPath.row];
    //文本宽度
    CGFloat labelW = self.tableView.bounds.size.width - 40;
    //标题高度
    CGRect titleSize = [suggestModel.title SizeWithTextFontSize:25
                                                      textWidth:labelW];
    //描述高度
    NSString *desc = suggestModel.content[@"desc"];
    CGRect subTitleSize = [desc SizeWithTextFontSize:15
                                           textWidth:labelW];
    //简介高度
    CGRect introSize = [suggestModel.intro SizeWithTextFontSize:22
                                                      textWidth:labelW];
    //文章/音乐/视频
    if (suggestModel.type.integerValue == 0
        || suggestModel.type.integerValue == 10
        || suggestModel.type.integerValue == 2) {
        
        return 355 + titleSize.size.height + subTitleSize.size.height + qouteImageSize.size.height;
    }
    //九宫格
    else if (suggestModel.type.integerValue == 1) {
        return 460 + titleSize.size.height + subTitleSize.size.height + introSize.size.height;
    }
    //大型画册
    else if (suggestModel.type.integerValue == 9) {
        return 420;
    }
    return 500;
}

//  cell 点击
- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    //  详情控制器
    WCG_M_DailyClickController *dailyClickVC = [[WCG_M_DailyClickController alloc] init];
    //  属性传值
    dailyClickVC.suggestModel = self.dailyContentArr[indexPath.row];
    dailyClickVC.titleStr = [self.dailyContentArr[indexPath.row] title];
    dailyClickVC.url = [self.dailyContentArr[indexPath.row] item_url];
    //  push
    [self.navigationController pushViewController:dailyClickVC
                                         animated:YES];
}

@end
